<?php
/**
 * 系统设置逻辑层
 * @author maniac
 */
namespace app\common\logic;

use app\common\model\Options AS OptionsModel;
use app\facade\UserLogs;

class System extends Options
{
    /**
     * 编辑
     * @param  array $data 请求的信息
     */
    public function save($data)
    {
        if (!trim($data['file']['attach_uri_prefix'])) {
            exception('请输入图片地址前缀');
        } elseif (!preg_match('/\S*\/$/', $data['file']['attach_uri_prefix'])) {
            exception('图片地址前缀必须以 / 结尾');
        }
        
        $db = $data['db'];
        
        // 数据表相关
        $db['is_reg']                 = (int)$db['is_reg'];
        $db['is_web']                 = (int)$db['is_web'];
        $db['reg_role_id']            = (int)$db['reg_role_id'];
        $db['is_post']                = (int)$db['is_post'];
        $db['first_post_time']        = (int)$db['first_post_time'];
        $db['wechat_reg']             = (int)$db['wechat_reg'];
        $db['upload_image_filesize']  = (int)$db['upload_image_filesize'];
        $db['upload_image_filetype']  = $db['upload_image_filetype'];
        $db['upload_attach_filesize'] = (int)$db['upload_attach_filesize'];
        $db['upload_attach_filetype'] = $db['upload_attach_filetype'];
        $db['points_rate']            = (int)$db['points_rate'];
        $db['register_points']        = (int)$db['register_points'];
        $db['order_points_rate']      = (float)$db['order_points_rate'];
        $db['points_buy_limit']       = (int)$db['points_buy_limit'];
        
        $update = [
            'id'           => $data['id'],
            'option_name'  => $data['option_name'],
            'option_value' => serialize($db),
        ];
        OptionsModel::update($update);
        $this->writeConfig($data['file']);
        UserLogs::insertLog(['title' => '修改网站设置', 'content' => "id={$data['id']}\r\noption_name={$data['option_name']}"]);
    }
    
    /**
     * 写入配置文件
     * @param  array $data 请求的信息
     */
    private function writeConfig($file)
    {
        $app_info_file = \think\facade\Env::get('config_path') . 'system.php';
        // 配置文件相关
        $file['login_nums']        = isset($file['login_nums']) && $file['login_nums'] ? (int)$file['login_nums'] : config('site.default_login_nums');
        $file['login_time']        = isset($file['login_time']) && $file['login_time'] ? (int)$file['login_time'] : config('site.default_login_time');
        $file['attach_uri_prefix'] = isset($file['attach_uri_prefix']) && $file['attach_uri_prefix'] ? $file['attach_uri_prefix'] : '/';
        $str = "<?php\n"
              ."/**\n"
              ." * 网站基本信息配置\n"
              ." * @author maniac\n"
              ." */\n"
              ."return [\n"
              ."\t'session_key'        => '".(config('system.session_key') ?: unid())."',\t\t// session 前缀密钥\n"
              ."\t'login_nums'         => '".$file['login_nums']."',\t\t// 登录次数限制\n"
              ."\t'login_time'         => '".$file['login_time']."',\t\t// 重新登录时间限制\n"
              ."\t'attach_uri_prefix'  => '".$file['attach_uri_prefix']."',\t\t// 图片地址前缀\n"
              ."];\n";
        file_put_contents($app_info_file, $str);
    }
}
